﻿/// <reference path="../jquery-1.4.1-vsdoc.js" />

(function ($) {


	$.fn.ShClientTabs = function (method) {

		var settings = {
			onSelect: false,
			onLoad: false
		};

		var methods = {
			init: function (options) {

				if (options) {
					$.extend(settings, options);
				}


				return this.each(function () {

					var $this = $(this),
					 data = $this.data('sh.clienttabs');

					// If the plugin hasn't been initialized yet
					if (!data) {

						// gather all tabs, and bind click to add remove tab selected class, then fire associated onselect, pass the current tab

						var $items = $this.find("li"),
							$selectedItem = $items.filter(".selected");

						$items.click(function (e) {
							e.preventDefault();
							var $t = $(this),
								onselect = $t.data("ontabselect");

							methods.select.call($this, $t); // global select function if exists
							// also look for onselect attribute and fire
							if (onselect != "" && typeof (onselect) == "function") {
								onselect.call($t, e);

							}

						});

						$this.data('sh.clienttabs', {
							target: $this,
							items: $items,
							selectedItem: $selectedItem,
							onSelect: settings.onSelect,
							onLoad: settings.onLoad
						});

						// fire onload
						(settings.onLoad) ? settings.onLoad($this.data('sh.clienttabs')) : null;
						// click selected item
						$selectedItem.click();

					}
				});


			},
			select: function (src) {

				// set src to be seelctedd, and reset "this" to non selected
				var s = this.data('sh.clienttabs');
				$.extend(s, { srcElement: src });

				s.items.removeClass("selected");
				src.addClass("selected");
				s.selectedItem = src;

				// fire onselect, broadcast the data info which contains the src
			
				(s.onSelect) ? s.onSelect(s) : null;
				return this;
			}

		};

		if (methods[method]) {
			return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
		} else if (typeof method === 'object' || !method) {
			return methods.init.apply(this, arguments);
		} else {
			$.error('Method ' + method + ' does not exist');
		}



	};


})(jQuery);
